package com.jweb.framework.core.channel.interceptors;
import java.util.Map;

import org.apache.ibatis.exceptions.IbatisException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.dao.DataAccessException;

import com.jweb.framework.core.Context;
import com.jweb.framework.core.CoreException;
import com.jweb.framework.core.channel.CommunicationException;
import com.jweb.framework.core.channel.MessagesProvider;

public class DefaultExceptionHandler implements ExceptionHandler{
	
	protected Logger logger = LoggerFactory.getLogger(DefaultExceptionHandler.class);
	private String errorType = "msgType";
	private String errorCodeName = "rspCode";
	private String errorMsgName = "rspMsg";
	private MessagesProvider messagesProvider ;
	private Map<String , String> messageCodeMapping ;
	private String defaultMappingCode ;
	
	public void setErrorType(String msgType){
		this.errorType = msgType ;
	}
	
	public void setErrorCodeName(String errorCodeName) {
		this.errorCodeName = errorCodeName;
	}

	public void setErrorMsgName(String errorMsgName) {
		this.errorMsgName = errorMsgName;
	}

	public void setMessagesProvider(MessagesProvider messagesProvider) {
		this.messagesProvider = messagesProvider;
	}

	public void setMessageCodeMapping(Map<String, String> messageCodeMapping) {
		this.messageCodeMapping = messageCodeMapping;
	}

	public void setDefaultMappingCode(String defaultMappingCode) {
		this.defaultMappingCode = defaultMappingCode;
	}

	public void handleException(Throwable throwable, Context context) {
		
		if(throwable == null){
			return ;
		}
		// getMessage() ���ش�throwable����ϸ��Ϣ�ַ�
		Object obj1 = throwable.getMessage(); // obj1="[SQ0001]"
		
		/*logger.debug(" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> exception message start >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		logger.debug("" , throwable.getCause());
		logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> exception message finish >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");*/
		
		String str = null ;
		Object obj3 ;
		if(throwable instanceof MessageSourceResolvable){
			obj3 = (MessageSourceResolvable)throwable ;
			if(((MessageSourceResolvable)obj3).getCodes() != null){
				obj1 = ((MessageSourceResolvable)obj3).getCodes()[0];
			}
			if(((MessageSourceResolvable)obj3).getDefaultMessage()!=null){
				str = ((MessageSourceResolvable)obj3).getDefaultMessage();
			}
		}
		
		if(throwable instanceof CoreException && ((CoreException)throwable).isWrapped()){
			throwable = throwable.getCause();
			
			if(throwable instanceof CommunicationException){
				if(((CommunicationException)throwable).isTimeout()){
					obj1 = "JAVACO0011";
				}
				else {
					obj1 = "JAVACO0012";
				}
			}
			else if(throwable instanceof DataAccessException || throwable instanceof IbatisException){
				obj1 = "JAVADB0001";
			}
			else {
				obj1 = "JAVADB0002" ;
			}
		}
		
		if(this.messageCodeMapping != null){
			obj3 = this.messageCodeMapping.get(obj1);
			if(obj3!=null){
				obj1 = obj3 ;
			}
			else if(this.defaultMappingCode!=null){
				obj1 = this.defaultMappingCode ;
			}
		}
		
		Object obj2 ; 
		try {
			
			obj2 = this.messagesProvider.resolveError(throwable, context.getLocale());
			
		} catch (Exception e) {
			if(str!=null){
				obj2 = str ;
			}
			else{
				obj2 = obj1 ;
			}
		}
		// ��Form�?�Ĵ����?�Դ�����������Ϊ��E
		/*if(("E" != context.getData(this.errorType))) {
			context.setData(this.errorType , "E");
		}*/
		context.setData(this.errorType , "E");
		context.setData(this.errorCodeName, obj1) ;
		context.setData(this.errorMsgName, obj2) ;
	}

}

